package com.zuo.demo01.controller.admin;


import com.alibaba.fastjson.JSON;
import com.zuo.demo01.config.Constants;
import com.zuo.demo01.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ConfigController {

    @Autowired
    private ConfigService configService;


    @RequestMapping("/img/upload")//头像上传
    @ResponseBody
    public String img_upload(@RequestParam("file") MultipartFile file,HttpSession session) throws Exception {
        Map<String,Object> map = new HashMap<>();
        File file1 = new File(Constants.FILE_IMGS_HEADER);
        for (File str ://删除之前的头像缓存
                file1.listFiles()) {
            if(str.exists()){
                str.delete();
            }
        }
        String pic = file.getOriginalFilename();
        String ff = Constants.FILE_IMGS_HEADER+"/"+pic;
        FileOutputStream fs=new FileOutputStream(ff);
        BufferedOutputStream bs=new BufferedOutputStream(fs);
        fs.write(file.getBytes());//获取上传的图片并保存到指定位置
        bs.close();
        fs.close();
        map.put("name","pic");
        map.put("value",pic);
        int result = configService.update(map);//修改数据库中的头像信息
        if(result == 0){
            map.put("msg","修改失败");
        }

        session.setAttribute("pic",pic);
        map.put("code",0);
        map.put("msg","修改成功");
        String remap = JSON.toJSONString(map);
        return remap;
    }


    @RequestMapping("/config/update3")
    @ResponseBody
    public String update_form3(@RequestParam("bottom_about") String bottom_about,@RequestParam("bottom_copy_right") String bottom_copy_right
                                ,@RequestParam("bottom_powered_by") String bottom_powered_by,@RequestParam("bottom_powered_by_url") String bottom_powered_by_url){
        int result = 1;
        Map<String,Object> map = new HashMap<>();

        map.put("name","bottom_about");
        map.put("value",bottom_about);
        result = configService.update(map);
        if(result == 0){
            return "底部About修改失败";
        }
        map.put("name","bottom_copy_right");
        map.put("value",bottom_copy_right);
        result = configService.update(map);
        if(result == 0){
            return "底部copy_right修改失败";
        }
        map.put("name","bottom_powered_by");
        map.put("value",bottom_powered_by);
        result = configService.update(map);
        if(result == 0){
            return "底部powered_by修改失败";
        }
        map.put("name","bottom_powered_by_url");
        map.put("value",bottom_powered_by_url);
        result = configService.update(map);
        if(result == 0){
            return "底部powered_by_url修改失败";
        }

        return "success";
    }

    @RequestMapping("/config/update2")
    @ResponseBody
    public String update_form2(@RequestParam("selfname") String selfname,@RequestParam("selfemail") String selfemail){
        int result = 1;
        Map<String,Object> map = new HashMap<>();

        map.put("name","selfname");
        map.put("value",selfname);
        result = configService.update(map);
        if(result == 0){
            return "个人名称修改失败";
        }
        map.put("name","selfemail");
        map.put("value",selfemail);
        result = configService.update(map);
        if(result == 0){
            return "个人邮箱修改失败";
        }

        return "success";
    }

}
